package com.sys.web.head.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AComment;
import com.sys.goods.pojo.ACommentEnum;
import com.sys.goods.service.IGoodsDbService;
import com.sys.users.pojo.AUsers;

/**
 * 商品前台操作
 * 
 * @author 
 */
@Controller
@RequestMapping(value = "/head/goods",produces = "text/html;charset=UTF-8")
public class GoodsHeadController extends BaseController
{
	@Autowired
	private IGoodsDbService goodsDbService;
	
	/**
	 * 评论列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/commentList")
	public String commentList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("status", ACommentEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AComment> apiResponse = this.goodsDbService.findCondListCommentService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/commentList";
	}

	/**
	 * 打开评论页面
	 * @param request
	 * @param response
	 */
	@RequestMapping("/commentInsert")
	public String commentInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("======commentInsert========");
		/* 页面跳转 */
		return "/head/goods/commentInsert" ; 
	}
	
	/**
	 * 评论提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/commentInsertSubmit")
	public String commentInsertSubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("======commentInsertSubmit=========");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		
		AComment comment = new AComment();
		String name = request.getParameter("name");
		String goodsId = request.getParameter("goodsId");
		String score = request.getParameter("score");
		String content = request.getParameter("content");
		
		comment.setName(name);
		comment.setScore(Byte.valueOf(score));
		comment.setGoodsId(Integer.valueOf(goodsId));
		comment.setContent(content);
		/* 创建对象 */
		comment.setUsersId(users.getId());
		
		ApiResponse<Object> apiResponseDb = this.goodsDbService.saveOneCommentService(comment);
		request.setAttribute("response", apiResponseDb.toJSON());
		return this.commentInsert(request,response);
	}

	/**
	 * 评论更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/commentUpdate")
	public String commentUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<AComment> apiResponse = this.goodsDbService.findOneCommentService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/commentInfo" ; 
	}
}
